<?
// /ui/commandhandler/request/group|renew|restore/group/[groupname][/status/ok]

class News_Command_ArticleRetriever
{
    var $Controller;
    var $Group; 
    var $Index; 
    function News_Command_ArticleRetriever($controller)
	{
	    $this->Controller = $controller;
        $this->Group      = $this->Controller->Settings['group'];  
        $this->Index      = $this->Controller->Settings['index']; 
        $this->Path       = "notify/{$this->Group}.{$this->Index}.get";
        $this->URI        = $this->Controller->URI (array('status'=>'ok'));
	}
	
	function GetURI()
	{
	    $uri = $this->Controller->URI (array('ui'=>'articlelist', 'request'=>NULL));
	    return array("RequestURI"  => $this->URI,
	                 "RedirectURI" => $uri);
	}
	
	function GetStatus()
	{
	    if (!file_exists($this->Path))
        {
            return 'OK';
        } 
        return '';
	}
	 
	function Signal()
	{   
        $message =  "{$this->Group}";
        
        if (strlen($this->Controller->Settings['status']) > 0)
        {
            return $this->GetStatus ();
        } 
        
        $this->Controller->Save($this->Path, $message); 
        return $this->Controller->Dialog ($this->GetURI());
	} 
	
	function Invoke($command)
	{   
	    $this->Path = $command['path'];
	    
        $nodeindex = $command['arg1'];
        $groupname = $this->Controller->Read($command['path']); 
        $filename = "data/{$groupname}.xml";
        if (file_exists($filename)) 
        {
		    $xml = simplexml_load_file($filename);
		    $result = $xml->xpath("//list/article[@index='{$nodeindex}']/items/article");
		        
		    $key = $result[0]->id;
		    $top = "data/perm/{$groupname}";
		    $dir = "data/perm/{$groupname}/{$key}";
		    echo "[{$top}]";
		    if (!is_dir($top)) mkdir ($top);
		    if (!is_dir($dir)) mkdir ($dir);
            $this->Controller->OpenConnection($groupname);
		    for ($x=0;$x<sizeof ($result);$x++)
		    { 
		        $picname = $this->Controller->GetArticle ($groupname, $result[$x]->id, $dir);
		        echo sprintf("%s of %s\n%s\n", $x + 1, sizeof($result), $picname);
		        $this->Controller->LogEntry ('Loading articles', $x + 1, sizeof($result), $groupname);
		    }      
           $this->Controller->Imager->Disconnect();
        }   
		unlink ($this->Path); 
	}
     
}



